package ch.subgate.pongwars;

import android.graphics.Canvas;
import android.graphics.Color;
import android.media.SoundPool;
import android.util.FloatMath;

public class Ball extends GameObject {
	
	public float bounceTime;
	
	public float direction;

    public float spin;

    public float speed;
	
	public Ball(float direction) {
        super();
		mX = Constants.DIC_BATTLEFIELD_WIDTH / 2.0f;
		mY = Constants.DIC_BATTLEFIELD_HEIGHT / 2.0f;
		bounceTime = Constants.BOUNCE_MIN;
		this.direction = direction;
        spin = 0.0f;
        speed = Constants.BOUNCE_MIN * Constants.SPEED_MULTIPLIER;
	}

    @Override
	public void update(float deltaTime) {
		float dx, dy;

        speed -= speed * Constants.FRICTION * deltaTime;
        direction += spin * deltaTime;
		
		dx = FloatMath.cos(direction) * speed * deltaTime;
		dy = FloatMath.sin(direction) * speed * deltaTime;
		
		mX += dx;
		mY -= dy;
	} // update

    @Override
    public void draw(Canvas c, GameContext gameContext) {
        mP.setColor(Color.BLACK);
        c.drawCircle(
                gameContext.battlefieldLeft + (mX * gameContext.unitWidth),
                gameContext.battlefieldTop + (mY * gameContext.unitWidth),
                0.5f * gameContext.unitWidth, mP);
    } // draw
	
	public void reset(float direction) {
		mX = Constants.DIC_BATTLEFIELD_WIDTH / 2.0f;
		mY = Constants.DIC_BATTLEFIELD_HEIGHT / 2.0f;
		bounceTime = Constants.BOUNCE_MIN;
		this.direction = direction;
        spin = 0.0f;
        speed = Constants.BOUNCE_MIN * Constants.SPEED_MULTIPLIER;
	} // reset

    public boolean bounce(Player p) {
        float ratio = (((p.mX - mX) / 2.0f) + (p.mSize / 2.0f)) / p.mSize;
        boolean superbounce = false;
        speed = bounceTime * Constants.SPEED_MULTIPLIER;
        spin = 0.0f;
        if (p instanceof HumanPlayer) {
            if (!((HumanPlayer)p).mIsCharging && ((HumanPlayer)p).mDischargeDelay < Constants.DISCHARGE_DELAY) {
                speed = (bounceTime + Constants.BOUNCE_CHARGE_MAX) * Constants.SPEED_MULTIPLIER;
                superbounce = true;
            } // if
            spin = p.mSpeed;
            ((HumanPlayer)p).mDischargeDelay = 0.0f;
        } else {
            if (((AIPlayer)p).trickShot == -1) { spin = -Constants.SPIN_MAX; }
            else if (((AIPlayer)p).trickShot == 1) { spin = Constants.SPIN_MAX; }
            else if (((AIPlayer)p).trickShot == 0) {
                speed = (bounceTime + Constants.BOUNCE_CHARGE_MAX) * Constants.SPEED_MULTIPLIER;
                superbounce = true;
            }
            ((AIPlayer)p).generatePlan();
        } // if

        if (spin > Constants.SPIN_MAX) spin = Constants.SPIN_MAX;
        else if (spin < -Constants.SPIN_MAX) spin = -Constants.SPIN_MAX;
        direction = (ratio * Constants.DIRECTION_MAX) + Constants.DIRECTION_MIN;

        if (p.mIsPlayerRed) {
            mY = p.mY + 1.0f;
            direction = -direction;
        } else {
            mY = p.mY - 1.0f;
        } // if

        if (bounceTime < Constants.BOUNCE_MAX) {
            bounceTime++;
        } // if

        return superbounce;
    } // bounce
	
} // Ball
